package com.kedou.sdk.ui.fragment;

import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.Fragment;


import com.kedou.sdk.ui.assist.OnPhoneVerifyCodeListener;
import com.kedou.sdk.ui.assist.OnRequestVerifyCodeListener;
import com.kedou.sdk.ui.assist.VerifyCodeCountDownTimer;
import com.kedou.sdk.utils.ResUtils;

/**
 * Created by Administrator on 2018/3/7.
 */

public abstract class PhoneVerifyFragment extends Fragment implements OnPhoneVerifyCodeListener {

    private VerifyCodeCountDownTimer mCountDownTimer;

    protected abstract Button getSendVerifyCodeView();

    protected abstract EditText getEditorVerifyCodeView();

    @Override
    public void onStop() {
        super.onStop();
        onCancelCountDownTimer();
    }

    @Override
    public void onStartCountDownTimer() {
        Button sendVerifyCodeBtn = getSendVerifyCodeView();
        if (sendVerifyCodeBtn.isClickable()) {
            sendVerifyCodeBtn.setClickable(false);
        }

        if (mCountDownTimer == null) {
            mCountDownTimer = new VerifyCodeCountDownTimer(sendVerifyCodeBtn);
        }
        mCountDownTimer.start();
    }

    @Override
    public void onCancelCountDownTimer() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }

        Button sendVerifyCodeBtn = getSendVerifyCodeView();
        if (!sendVerifyCodeBtn.isClickable()) {
            sendVerifyCodeBtn.setClickable(true);
            sendVerifyCodeBtn.setText(ResUtils.getStringIdentifier(getActivity(), "hnssn_get_captcha"));
        }
    }

    @Override
    public void onCallbackVerifyCode(String verifyCode) {
        getEditorVerifyCodeView().setText(verifyCode);
    }

    private OnRequestVerifyCodeListener getOnRequestVerifyCodeListener() {
        Activity activity = getActivity();
        if (activity instanceof OnRequestVerifyCodeListener) {
            return (OnRequestVerifyCodeListener) activity;
        }
        return null;
    }

    protected void requestVerifyCode(String verify, String verifyType) {
        OnRequestVerifyCodeListener listener = getOnRequestVerifyCodeListener();
        if (listener != null) {
            listener.doRequestPhoneVerifyCode(verify, verifyType);
        }
    }

}
